package nbcp.web.open

import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.servlet.ModelAndView
import nbcp.base.comm.*
import nbcp.base.extend.*
import nbcp.base.utils.MyUtil
import nbcp.comm.JsonpMapping
import nbcp.db.mongo.entity.AdminLoginUser
import nbcp.db.mongo.entity.AdminUser
import nbcp.db.mongo.entity.SysLoginUser
import nbcp.db.mongo.entity.SysUser
import nbcp.db.mongo.mor
import nbcp.db.mongo.match
import nbcp.db.mongo.query
import nbcp.db.mongo.repository.sys.findByLoginName
import nbcp.web.UserId
import nbcp.web.UserName
import org.springframework.beans.factory.annotation.Autowired
import springfox.documentation.annotations.ApiIgnore
import java.io.File
import java.util.*
import javax.servlet.http.HttpServletRequest

/**
 * Created by udi on 17-5-23.
 */

@OpenAction
@RestController
@JsonpMapping("")
class Open_Home_Controller {
    companion object {
        private val logger = LoggerFactory.getLogger(this::class.java.declaringClass)
    }

    @Value("\${spring.profiles.active}")
    var profiles: String = "";


    @RequestMapping("/hi", method = arrayOf(RequestMethod.GET, RequestMethod.POST))
    fun hi(loginName: String, cityCode: String, session: HttpServletRequest): ApiResult<JsonMap> {
        var ret = ApiResult<JsonMap>()
        ret.msg = "";
        ret.data = JsonMap();

        var jarFile = MyUtil.getStartingJarFile();
        ret.data!!["发版时间"] = Date(jarFile.lastModified()).AsString();
        ret.data!!["当前配置"] = profiles;
        ret.data!!["登录用户Id"] = session.UserId;
        ret.data!!["登录用户名称"] = session.UserName;

        if (loginName.HasValue) {
            ret.data!![loginName] = mor.system.sysUser.findByLoginName(loginName)?.name.AsString("null");
        }

//        ret.data!!["ValidateCode"] = session.session.ValidateCode;
        return ret;
    }

    @ApiIgnore
    @RequestMapping("/init", method = arrayOf(RequestMethod.GET))
    fun init(request: HttpServletRequest, session: HttpServletRequest): String {
        if (mor.admin.adminUser.query()
                        .where { it.loginName match "admin" }
                        .exists() == false) {
            var admin = AdminUser();
            admin.loginName = "admin";
            admin.name = "管理员"

            mor.admin.adminUser.insert(admin);
        }

        if (mor.admin.adminLoginUser.query()
                        .where { it.loginName match "admin" }
                        .exists() == false) {
            var admin_login = AdminLoginUser();
            admin_login.loginName = "admin"
            admin_login.password = "JdVa0oOqQAr0ZMdtcTwHrQGl";

            mor.admin.adminLoginUser.insert(admin_login);
        }

        return "OK";
    }

//    @GetMapping("/doc")
//    fun doc(): ModelAndView = ModelAndView("redirect:/swagger-ui.html");
}
